package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjq.allservice.entity.Swiper;
import com.cjq.allservice.entity.Type;
import com.cjq.allservice.entity.vo.SwiperQuery;
import com.cjq.allservice.entity.vo.TypeQuery;
import com.cjq.allservice.service.SwiperService;
import com.cjq.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-01-21
 */
@RestController
@RequestMapping("/allservice/swiper")
@CrossOrigin
public class SwiperController {

    @Autowired
    private SwiperService swiperService;

    @ApiOperation("进行轮播图的上传")
    @PostMapping("/addSwiper")
    public R addSwiper(@RequestBody Swiper swiper){
        boolean save = swiperService.save(swiper);
        if (save==true){
            return R.succ().message("上传成功！");
        }else {
            return R.fail().message("上传失败！");
        }
    }

    @ApiOperation("获取首页轮播图")
    @GetMapping("/getAllSwiper")
    public R getAllSwiper(){
        List<Swiper> list = swiperService.list();
        return R.succ().data("swiperList",list);
    }

    @ApiOperation("删除轮播图")
    @DeleteMapping("/deleteSwiper")
    public R deleteSwiper(@RequestParam String swiperId){
        boolean b = swiperService.removeById(swiperId);
        if (b==true){
            return R.succ().message("删除成功！");
        }else {
            return R.fail().message("删除失败！");
        }
    }

    @GetMapping("/getSwiperById")
    public R getSwiperById(@RequestParam String id){
        Swiper swiper = swiperService.getById(id);
        return R.succ().data("swiperInfo",swiper);
    }

    @PostMapping("/updateSwiper")
    public R updateSwiper(@RequestBody Swiper swiper){
        boolean b = swiperService.updateById(swiper);
        if (b==true){
            return R.succ().message("修改成功！");
        }else {
            return R.fail().message("修改失败！");
        }
    }

    @ApiOperation("条件查询类目带分页")
    //条件查询带分页
    @PostMapping ("pageSwiperCondition/{current}/{limit}")
    public R pageDoctorsCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) SwiperQuery swiperQuery){
        Page<Swiper> page=new Page<>(current,limit);
        QueryWrapper<Swiper> wrapper = new QueryWrapper<>();

        //多条件组合查询
        String id = swiperQuery.getSwiperId();
        String name = swiperQuery.getSwiperName();
        if (!StringUtils.isEmpty(id)){
            wrapper.like("swiper_id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("swiper_name",name);
        }
        wrapper.orderByDesc("update_time");

        swiperService.page(page,wrapper);
        long total=page.getTotal();
        System.out.println(total);
        List<Swiper> records = page.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.succ().data(map);
    }

}

